/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tp1.controllers;

import java.sql.SQLException;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import tp1.dao.PlanoDao;
import tp1.models.Plano;
import tp1.views.PlanoPanel;

/**
 *
 * @author tulio
 */
public class PlanoController extends Controller {

    protected static PlanoController instance = null;

    public static PlanoController getInstance(int controller, JPanel view){
        if(instance == null) {
            instance = new PlanoController(view);
        }
        return instance;
    }

    protected PlanoController(JPanel view) {
        super(view);
    }

    @Override
    public void action(int event) {
        switch(event){
            case Controller.EVENTO_ABRIR_TELA_PLANO:
                abrirAction();
                break;
            case Controller.EVENTO_SALVAR_PLANO:
                salvarAction();
                break;
            case Controller.EVENTO_APAGAR_PLANO:
                apagarAction();
                break;
            case Controller.EVENTO_NOVO_PLANO:
                novoAction();
                break;
            case Controller.EVENTO_EDITAR_PLANO:
                editarAction();
                break;
            case Controller.EVENTO_BUSCAR_PLANO:
                buscarAction();
                break;
        }
    }

    private void salvarAction() {
        PlanoPanel planoPanel = (PlanoPanel) this.view;

        Plano plano = new Plano(Integer.parseInt(planoPanel.getFieldCodigo().getText()));
        plano.setDescricao(planoPanel.getFieldDescricao().getText());
        plano.setFranquia(Integer.parseInt(planoPanel.getFieldFranquia().getText()));
        plano.setValorDaChamada(Float.parseFloat(planoPanel.getFieldValorChamada().getText()));
        plano.setValorMensal(Float.parseFloat(planoPanel.getFieldValorMensal().getText()));

        try {
            if(PlanoDao.save(plano) == 1){
                planoPanel.updateGridData();
                planoPanel.getFieldCodigo().setEditable(false);
                JOptionPane.showMessageDialog(planoPanel, "Registro salvo com sucesso.");
            } else {
                JOptionPane.showMessageDialog(planoPanel, "Nenhum registro salvo. Código inválido.");
            }
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(planoPanel, ex.getMessage(), "Erro!", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void buscarAction() {
        PlanoPanel planoPanel = (PlanoPanel) this.view;
        String value = JOptionPane.showInputDialog(planoPanel, "Qual é o código do plano que deseja localizar?", "Busca", JOptionPane.QUESTION_MESSAGE);
        Object[][] data;
        try {
            data = PlanoDao.findAsVector(Integer.parseInt(value));
            planoPanel.setGridData(data);
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(planoPanel, ex.getMessage());
        }
    }

    private void editarAction() {
        PlanoPanel planoPlano = (PlanoPanel) this.view;

        if(planoPlano.getGridPlano().getSelectedRow() != -1){
            planoPlano.getTabs().setSelectedIndex(1);
        } else {
            JOptionPane.showMessageDialog(planoPlano, "Nenhum linha selecionada. Para editar, primeiro selecione uma linha.");
        }
    }

    private void novoAction() {
        PlanoPanel planoPanel = (PlanoPanel) this.view;

        planoPanel.getFieldCodigo().setText("");
        planoPanel.getFieldCodigo().setEditable(true);
        planoPanel.getFieldDescricao().setText("");
        planoPanel.getFieldFranquia().setText("");
        planoPanel.getFieldValorChamada().setText("");
        planoPanel.getFieldValorMensal().setText("");

        planoPanel.getTabs().setSelectedIndex(1);
    }

    private void apagarAction() {
        PlanoPanel planoPanel = (PlanoPanel) this.view;
        try {
            int row = planoPanel.getGridPlano().getSelectedRow();
            Object codigo = planoPanel.getGridPlano().getModel().getValueAt(row, 0);
            if(PlanoDao.delete(codigo.toString()) == 1){
                planoPanel.updateGridData();
                JOptionPane.showMessageDialog(planoPanel, "Registro apagado com sucesso.");
            } else {
                JOptionPane.showMessageDialog(planoPanel, "Nenhum registro apagado. Código inválido.");
            }
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(planoPanel, ex.getMessage(), "Erro!", JOptionPane.ERROR_MESSAGE);
        }
    }

}
